# shellcheck shell=bash disable=SC2166,2012,2034,2207,1090,2076
___x_cmd_advise_man(){
    local X_help_cmd; X_help_cmd='x help -m advise man' help:arg-null:parse
    local op="$1"; shift
    case "$op" in
        ls)             ___x_cmd_advise_man_ls "$@"         ;;
        update)         ___x_cmd_advise_man_update "$@"     ;;
        which)          ___x_cmd_advise_man_which "$@"      ;;
        load)           ___x_cmd_advise_man_load "$@"       ;;
        rm)             ___x_cmd_advise_man_rm "$@"         ;;
        *)              ___x_cmd_advise_man_load "$@"       ;;
    esac
}

___X_CMD_ADVISE_MAN_PKG_VERSION="v0.0.1"
___X_CMD_ADVISE_MAN_PKG="$___X_CMD_ROOT_SHARED/advise/$___X_CMD_ADVISE_MAN_PKG_VERSION"
___X_CMD_ADVISE_MAN_XCMD_PKG="$___X_CMD_ADVISE_MAN_PKG/all.tar.xz"
___X_CMD_ADVISE_MAN_COMPLETIONS_PKG="$___X_CMD_ADVISE_MAN_PKG/$___X_CMD_CUR_SHELL-completion.tar.xz"

___X_CMD_ADVISE_MAN_XCMD_FOLDER="$___X_CMD_ROOT_DATA/advise/xcmd/$___X_CMD_ADVISE_MAN_PKG_VERSION"
___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER="$___X_CMD_ROOT_DATA/advise/completion/$___X_CMD_ADVISE_MAN_PKG_VERSION/$___X_CMD_CUR_SHELL-completion"

___x_cmd_is_suitable_advise_completion_env(){
    ___x_cmd_is_suitable_advise_env || return 1
    # bash >= 4.2
    [ -z "$BASH_VERSION" ] || [ "${BASH_VERSINFO[0]}" -gt 4 ] || [ "${BASH_VERSINFO[0]}" -eq 4 -a "${BASH_VERSINFO[1]}" -ge 2 ] || return 1
}

if [ -n "$BASH_VERSION" ] && ___x_cmd_is_suitable_advise_completion_env ; then  ___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER_SRC="$___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER/completions"
elif [ -n "$ZSH_VERSION" ];then ___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER_SRC="$___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER/src"
fi

___x_cmd_advise_man___folder_exist_xcmd(){
    [ -d "$___X_CMD_ADVISE_MAN_XCMD_FOLDER" ]
}
___x_cmd_advise_man___folder_exist_completion(){
    [ -d "$___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER" ]
}

# Section: advise man install
xrc websrc; ___x_cmd_websrc load
___x_cmd_advise_man___pkg_download_inner_url_(){
    local owner_repo="$1"
    local tar_name="$2"
    if [ "$___X_CMD_WEBSRC_REGION" != cn ]; then
        x_gitdomain='github.com';
        x_url="https://${x_gitdomain}/${owner_repo}/releases/download/${___X_CMD_ADVISE_MAN_PKG_VERSION}/${tar_name}"
    else
        x_gitdomain='gitcode.net'
        x_url="https://${x_gitdomain}/${owner_repo}/-/releases/${___X_CMD_ADVISE_MAN_PKG_VERSION}/downloads/${tar_name}"
    fi
}

___x_cmd_advise_man___pkg_download_inner(){
    local owner_repo="$1"
    local tar_name="$2"
    local pkg_path="$3"

    local x_gitdomain; local x_url
    ___x_cmd_advise_man___pkg_download_inner_url_ "$owner_repo" "$tar_name"
    advise:info "Download $tar_name from $x_gitdomain $owner_repo"
    advise:debug "Url => $x_url"
    x ensurefp "$pkg_path.tmp"
    x curl -L -sS --retry-max-time 10 --retry 0 "$x_url" --output "$pkg_path.tmp" || {
        advise:error "Fail to download $tar_name from $x_gitdomain $owner_repo"
        return 1
    }
    x rmrf "$pkg_path"
    x mv "$pkg_path.tmp" "$pkg_path"
}

___x_cmd_advise_man___pkg_is_no_update(){
    [ -f "$1" ] || return
    x fileage --in 3d "$1"
}

___x_cmd_advise_man___pkg_is_required_update(){
    ___x_cmd_advise_man___pkg_is_no_update "$___X_CMD_ADVISE_MAN_XCMD_PKG" || return 0
    ! ___x_cmd_is_suitable_advise_completion_env || ___x_cmd_advise_man___pkg_is_no_update "$___X_CMD_ADVISE_MAN_COMPLETIONS_PKG" || return 0
    return 1
}

___x_cmd_advise_man___pkg_check_update_completion(){
    ___x_cmd_advise_man___pkg_is_no_update "$___X_CMD_ADVISE_MAN_COMPLETIONS_PKG" || ___x_cmd_advise_man___pkg_unzip_completion --update || return
}

___x_cmd_advise_man___pkg_check_update_xcmd(){
    ___x_cmd_advise_man___pkg_is_no_update "$___X_CMD_ADVISE_MAN_XCMD_PKG" || ___x_cmd_advise_man___pkg_unzip_xcmd --update || return
}

___x_cmd_advise_man___pkg_download_completion(){
    ___x_cmd_is_suitable_advise_completion_env || return 1
    ___x_cmd_advise_man___pkg_download_inner x-cmd-build/completion "${___X_CMD_CUR_SHELL}-completion.tar.xz" "$___X_CMD_ADVISE_MAN_COMPLETIONS_PKG"
}

___x_cmd_advise_man___pkg_download_xcmd(){
    ___x_cmd_advise_man___pkg_download_inner x-cmd/advise all.tar.xz "$___X_CMD_ADVISE_MAN_XCMD_PKG"
}

___x_cmd_advise_man___pkg_unzip_completion(){
    ___x_cmd_is_suitable_advise_completion_env || return 1
    { [ "$1" != --update ] && [ -f "$___X_CMD_ADVISE_MAN_COMPLETIONS_PKG" ]; } || ___x_cmd_advise_man___pkg_download_completion || return 1

    x rmrf "$___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER"; x mkdirp "$___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER"
    ! x uz "$___X_CMD_ADVISE_MAN_COMPLETIONS_PKG" "${___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER}/../" || advise:debug "Decompress advise/$___X_CMD_CUR_SHELL-completion.tar.xz to $___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER"
}

___x_cmd_advise_man___pkg_unzip_xcmd(){
    { [ "$1" != --update ] && [ -f "$___X_CMD_ADVISE_MAN_XCMD_PKG" ] ; } || ___x_cmd_advise_man___pkg_download_xcmd || return

    x rmrf "$___X_CMD_ADVISE_MAN_XCMD_FOLDER";   x mkdirp "$___X_CMD_ADVISE_MAN_XCMD_FOLDER"
    ! x uz "$___X_CMD_ADVISE_MAN_XCMD_PKG" "$___X_CMD_ADVISE_MAN_XCMD_FOLDER" || advise:debug "Decompress advise/all.tar.xz to $___X_CMD_ADVISE_MAN_XCMD_FOLDER"
}

# EndSection

___x_cmd_advise_man_update(){
    local X_help_cmd='x help -m advise man update'
    help:arg:parse
    case "$1" in
        x-cmd)          ___x_cmd_advise_man___pkg_unzip_xcmd --update       ;;
        completion)     ___x_cmd_advise_man___pkg_unzip_completion --update ;;
        x.advise.jso)   ___x_cmd_advise_man_gen_x_advise_force              ;;
        all)            ___x_cmd_advise_man___pkg_unzip_xcmd --update
                        ___x_cmd_advise_man___pkg_unzip_completion --update
                        ___x_cmd_advise_man_gen_x_advise_force
                        ;;
        *)              ___x_cmd_advise_man___pkg_check_update_xcmd
                        ___x_cmd_advise_man___pkg_check_update_completion
                        ;;
    esac
}

___x_cmd_advise_man_ls(){
    local X_help_cmd='x help -m advise man ls'
    help:arg:parse
    ___x_cmd_advise_man___folder_exist_xcmd || ___x_cmd_advise_man___pkg_unzip_xcmd || return
    case "$1" in
        x-cmd)              x fsiter --folder "$___X_CMD_ADVISE_MAN_XCMD_FOLDER" ;;
        npx)                x ls "$___X_CMD_ADVISE_MAN_XCMD_FOLDER/npx/_" ;;
        pipx)               x ls "$___X_CMD_ADVISE_MAN_XCMD_FOLDER/pipx/_" ;;
        docker)             x ls "$___X_CMD_ADVISE_MAN_XCMD_FOLDER/docker/_" ;;
        completion)         ___x_cmd_is_suitable_advise_completion_env || {
                                help:show:ret:64
                            }
                            if ___x_cmd_advise_man___folder_exist_completion || ___x_cmd_advise_man___pkg_unzip_completion; then
                                x ls "$___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER_SRC" 2>/dev/null
                            fi
                            ;;
        *)                  help:show:ret:64 ;;
    esac
}

# Section: which
___x_cmd_advise_man_which___completion_getfile_(){
    x_="$___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER_SRC/$1"
    [ -f "$x_" ] || x_="$___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER_SRC/_$1"
}

___x_cmd_advise_man_which___xcmd(){
    local X_help_cmd; X_help_cmd='x help -m advise man which x-cmd' help:arg:parse
    if ___x_cmd_advise_man___folder_exist_xcmd || ___x_cmd_advise_man___pkg_unzip_xcmd; then
        local file; local IFS=" "; for file in "$1/advise.t.jso" "npx/_/$1/advise.t.jso" "pipx/_/$1/advise.t.jso"; do
            file="$___X_CMD_ADVISE_MAN_XCMD_FOLDER/$file"
            [ -f "$file" ] || continue
            printf "%s\n" "$file"
            return 0
        done
        printf "%s\n" "$___X_CMD_ADVISE_MAN_XCMD_FOLDER"
        return 1
    fi
}

___x_cmd_advise_man_which___completion(){
    local X_help_cmd; X_help_cmd='x help -m advise man which completion' help:arg:parse
    ___x_cmd_is_suitable_advise_completion_env || return 1
    if ___x_cmd_advise_man___folder_exist_completion || ___x_cmd_advise_man___pkg_unzip_completion; then
        [ -n "$1" ] || {
            printf "%s\n" "$___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER_SRC"
            return
        }

        local x_=; ___x_cmd_advise_man_which___completion_getfile_ "$1"
        [ -f "$x_" ] || return
        printf "%s\n" "$x_"
    fi
}

___x_cmd_advise_man_which(){
    local X_help_cmd='x help -m advise man which'
    help:arg:parse
    case "$1" in
        x-cmd)          shift; ___x_cmd_advise_man_which___xcmd "$@" ;;
        completion)     ___x_cmd_is_suitable_advise_completion_env || {
                            help:show:ret:64
                        }
                        shift; ___x_cmd_advise_man_which___completion "$@" ;;
        *)              help:show:ret:64 ;;
    esac
}

# EndSection

# Section: load
___x_cmd_advise_man_load___xcmd_one(){
    if [ -n "$ZSH_VERSION" ];then                       compdef "___x_cmd_advise_man_xcmd_completer" "$@"
    elif [[ "$BASH_VERSION" =~ (^3.*|^4.[0-3]) ]]; then complete -F "___x_cmd_advise_man_xcmd_completer" "$@"
    else                                                complete -o nosort -F "___x_cmd_advise_man_xcmd_completer" "$@"
    fi
}

___x_cmd_advise_man_load___xcmd_all(){
    ___x_cmd_advise_man___folder_exist_xcmd || return
    advise:debug "Load x-cmd/advise from $___X_CMD_ADVISE_MAN_XCMD_FOLDER"
    eval "$(___x_cmd_advise_man_ls x-cmd | command awk '
        {                           code = code " " $1;                                         }
        (NR % 50 == 0){             print "___x_cmd_advise_man_load___xcmd_one" code;      code = ""   }
        END {   if (code != "")     print "___x_cmd_advise_man_load___xcmd_one" code;                  }
    ')"
}

___x_cmd_advise_man_load___xcmd(){
    local X_help_cmd; X_help_cmd='x help -m advise man load x-cmd' help:arg:parse
    ___x_cmd_advise_man___folder_exist_xcmd || ___x_cmd_advise_man___pkg_unzip_xcmd || return
    if [ "$#" -ge 1 ]; then ___x_cmd_advise_man_load___xcmd_one "$@"
    else ___x_cmd_advise_man_load___xcmd_all
    fi
}

___x_cmd_advise_man_load___completion(){
    ___x_cmd_is_suitable_advise_completion_env || return
    ___x_cmd_advise_man___folder_exist_completion || ___x_cmd_advise_man___pkg_unzip_completion || return
    ___x_cmd_advise_man_load___completion_main
}

___x_cmd_advise_man_load___completion_main(){
    ___x_cmd_is_suitable_advise_completion_env || return
    if ___x_cmd_advise_man___folder_exist_completion; then
        local filename="$___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER/completion.sh"
        if [ -f "$filename" ]; then
            advise:debug "Load $___X_CMD_CUR_SHELL completion from $filename"
            .  "$filename"
        fi
    fi
}

___x_cmd_advise_man_load___x(){
    advise:debug "Load x advise of $___X_CMD_ADVISE_MAN_CACHE_X_ADVISE_JSO"
    ___x_cmd_advise_man_gen_x_advise
    ___x_cmd_advise init x "$___X_CMD_ADVISE_MAN_CACHE_X_ADVISE_JSO"
}

___x_cmd_advise_man_load___all_check_update(){
    ! ___x_cmd_is_suitable_advise_completion_env || ___x_cmd_advise_man___folder_exist_completion || ___x_cmd_advise_man___pkg_unzip_completion
    ___x_cmd_advise_man___folder_exist_xcmd || ___x_cmd_advise_man___pkg_unzip_xcmd
    ___x_cmd_advise_man_gen_x_advise
}

___x_cmd_advise_man_load___all(){
    ( name="advise.man.bg" x bgsingleton run --wait ___x_cmd_advise_man_load___all_check_update )
    ___x_cmd_advise_man_load___completion_main
    ___x_cmd_advise_man_load___xcmd_all
    ___x_cmd_advise_man_load___x
}

___x_cmd_advise_man_load(){
    local X_help_cmd='x help -m advise man load'
    help:arg:parse
    case "$1" in
        x-cmd)          shift; ___x_cmd_advise_man_load___xcmd "$@" ;;
        completion)     ___x_cmd_is_suitable_advise_completion_env || {
                            help:show:ret:64
                        }
                        ___x_cmd_advise_man_load___completion ;;
        all)            ___x_cmd_advise_man_load___all ;;
        *)              help:show:ret:64 ;;
    esac
}

# EndSection

___x_cmd_advise_man_rm(){
    local X_help_cmd='x help -m advise man rm'
    help:arg:parse
    local x_=
    case "$1" in
        x-cmd)
            shift; X_help_cmd='x help -m advise man rm x-cmd' help:arg:parse
            if ___x_cmd_advise_man___folder_exist_xcmd; then
                x_="$___X_CMD_ADVISE_MAN_XCMD_FOLDER/$1"
                [ -d "$x_" ] || return
                advise:warn "Remove $x_"
                x rmrf "$x_"
            fi
            ;;
        completion)
            shift; X_help_cmd='x help -m advise man rm completion' help:arg:parse
            if ___x_cmd_advise_man___folder_exist_completion; then
                if [ "$#" -eq 0 ]; then x_="$___X_CMD_ADVISE_MAN_COMPLETIONS_FOLDER_SRC"
                else ___x_cmd_advise_man_which___completion_getfile_ "$1"
                fi
                [ -e "$x_" ] || return
                advise:warn "Remove $x_"
                x rmrf "$x_"
            fi
            ;;
        *)  help:show:ret:64 ;;
    esac
}

___x_cmd_advise_man_xcmd_completer() {
    # Interesting ... Passing
    ___X_CMD_ADVISE_RUN_CMD_FOLDER="$___X_CMD_ADVISE_MAN_XCMD_FOLDER" ___x_cmd_advise_run ""
}

## Section man advise
___x_cmd_advise_man__list(){
    local IFS="$___X_CMD_ADVISE_IFS_INIT"
    candidate_exec_arr=( $(___x_cmd_advise_man_ls "${1:-x-cmd}") )
}
## EndSection
